package br.com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.bean.BairroBean;
import br.com.bean.CidadeBean;
import br.com.controller.*;
import br.com.connection.*;
import br.com.view.BairroView;
import br.com.view.VeiculoView;

//@WebServlet("/in_log/cidades")
public class Bairro extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public Bairro() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	doeExecute(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doeExecute(request, response);
	}
	protected void doeExecute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		StringBuilder sb = new StringBuilder();
		String typy="Bairro";
		CadWindows c = new CadWindows();
		BairroView bai = new 	BairroView();
ControllerBairro bairroController = new ControllerBairro();
		try {
			Connection conn = new ConnectionFactory().getConnection();
			
			
			out.print(bai.bairro());
			c.doExecute(request, response);
			String nome= request.getParameter("nome");
			String cidade= request.getParameter("cidade");
			Integer cidadeInt = Integer.parseInt(cidade);
			bairroController.inserir(conn, nome, cidadeInt);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	public void busca(HttpServletRequest request, HttpServletResponse response){
		try {
			Connection conn = new  ConnectionFactory().getConnection();
			PrintWriter out = response.getWriter();
			String param1 = request.getParameter("cidades");
			int codigo = Integer.parseInt(param1);	
			List<BairroBean> bar = ControllerBairro.listBairro(conn, codigo);
			for (BairroBean bairro : bar) {
				out.print("<option value='"+bairro.getId()+"'>"+bairro.getNome()+"</option>\n");
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
}}
